package com.seeyon.apps.ext.cop.button.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import java.util.Map.Entry;

import com.seeyon.apps.snail.file.FileHelper;
import com.seeyon.ctp.common.SystemEnvironment;
import com.seeyon.ctp.organization.event.AddDepartmentEvent;
import snail.common.utils.IDHelper;
import snail.common.utils.JSONHelper;
import snail.common.utils.MapHelper;
import snail.common.utils.StringHelper;
import snail.common.utils.cipher.MD5Coder;
import snail.common.web.HTTPHelper;
import snail.common.web.HTTPRequestHelper;

public class TestBaiwang {

	public static void main(String[] args) throws Exception {
		
		HTTPHelper.isDebug();
		query();
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> query()  {
		
		String timestamp = String.valueOf(System.currentTimeMillis());
		System.out.println(timestamp);
		String appKey = "10002840";
		String secret = "fbe50cc0-1200-4e10-9ec6-4931c39048b9";
		String token = getToken(appKey, secret, timestamp);
		
		Map<String, Object> params = MapHelper.builder("method", "baiwang.image.invoices.query")
				.put("appKey", "10002840")
				.put("token", token)
				.put("timestamp", timestamp)
				.put("format", "json")
				.put("version", "6.0")
				.put("type", "sync")
				.put("requestId", IDHelper.getID())
				.body();
		
		Map<String, Object> header = MapHelper.builder("Content-Type", "application/json").body();
		
		/**
		 * 样例：{"userAccount":"admin_3000000241865","tenantId":"3000000084718"}
		 */
		/*String body = MapHelper.builder("userAccount", "admin_3000000994523")
				.put("tenantId", "3000000607432")
				.put("inspectionStatus", 1)
				.toJson();*/
		Date date = new Date();
		Calendar calendar1 = Calendar.getInstance();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		calendar1.add(Calendar.DATE, -3);
		String three_days_ago = sdf1.format(calendar1.getTime());
		System.out.println(three_days_ago);
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String date1 = dateFormat.format(date);
		System.out.println(dateFormat.format(date));
		SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		String date2 = dateFormat1.format(date);
		System.out.println(date2);
		String body = MapHelper.builder("userAccount", "admin_3000000994523")
				.put("tenantId", "3000000607432")
				.put("createTimeStart", three_days_ago)
				.put("createTimeEnd", date1)
				.put("inspectionStatus", 1)
				.toJson();

		String sign = getSign(params, secret, body);
		
		String url = "https://openapi.baiwang.com/router/rest";
		
		for(Entry<String, Object> p : params.entrySet()) {
			
			url = HTTPRequestHelper.join(url, p.getKey(), String.valueOf(p.getValue()));
		}
		
		url = HTTPRequestHelper.join(url, "sign", sign);

		Map remap = (Map<String, Object>) JSONHelper.parse(HTTPHelper.post(url, body, header));

		Object invoiceDetailList = remap.get("response");

		String  a = invoiceDetailList.toString();



		List listmap = MapHelper.getList(remap, "response");



		
		return remap;


	}
	
	/**
	 * 给TOP请求签名。
	 *
	 * @param params 所有字符型的TOP请求参数
	 * @param secret 签名密钥
	 * @return 签名
	 * @throws Exception
	 */
	public static String getSign(Map<String, Object> params, String secret, String body) {

		// 第一步：检查参数是否已经排序
		ArrayList<String> keys = new ArrayList<String>(params.keySet());

		Collections.sort(keys);

		// 第二步：把所有参数名和参数值串在一起
		StringBuilder query = new StringBuilder();

		query.append(secret);

		for (String key : keys) {

			Object value = params.get(key);

			if (StringHelper.isNotBlank(key) && StringHelper.isNotBlank(value)) {

				query.append(key).append(value);
			}
		}
		
		System.out.println(query);

		query.append(body);

		query.append(secret);



		return MD5Coder.generateMD5(query.toString()).toUpperCase();
	}
	
	@SuppressWarnings("unchecked")
	public static String getToken(String appKey, String secret, String timestamp) {
		
		/**
		 *  样例：http://60.205.83.27/router/rest?password=02ce60f12399deb84f3db4b0c0a7a8acab5b70eb&method=baiwang.oauth.token&grant_type=password
		 *  						&client_secret=c8e08a17-973a-49f0-a3c6-39274dca2da7&version=6.0&client_id=1001982&username=admin_3000000241865&timestamp=1652231671
		 */
		
		String url = "https://openapi.baiwang.com/router/rest";
		url = HTTPRequestHelper.join(url, "timestamp", timestamp);
		url = HTTPRequestHelper.join(url, "method", "baiwang.oauth.token");
		url = HTTPRequestHelper.join(url, "grant_type", "password");
		url = HTTPRequestHelper.join(url, "version", "6.0");
		
		url = HTTPRequestHelper.join(url, "client_id", appKey);



		String body = MapHelper.builder("username", "admin_3000000994523")
				.put("password", "be86bd91aa13a15a05a16bfaf79fbe4afb2a859c")
				.put("client_secret", secret)
				.toJson();
		Map<String, Object> header = MapHelper.builder("Content-Type", "application/json").body();

		Map<String, Object> result = (Map<String, Object>) JSONHelper.parse(HTTPHelper.post(url, body, header));

		//Map<String, Object> result = (Map<String, Object>) JSONHelper.parse(HTTPHelper.post(url));
		
		return MapHelper.getString(result, "response.access_token");
	}

}
